package GUI;
/**
 * Exceptional request to add a student to a course that his class is not registered to
 * In this panel the administration selects to which course the student wants to register
 * and sends the request to the principle for authorization 
 * 
 */

import java.awt.Dimension;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JButton;

import Common.Course;
import Common.StudentClass;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Vector;

public class StudentAdditionToCourse extends JPanel {
	private JLabel lblExceptionalRequest = null;
	private JLabel lblSelectCourse = null;
	private JComboBox cBSelCourse = null;
	public Login myLoginFrame = null;
	public UserAdmin user=null;
	public AdminMainMenu amn=null;	
	private JButton btnRequestAuthorization = null;
	private StudentAdditionToCourse selCrsForExAdd = null;
	private StudentClass std = null;
	private Vector<RawCourse> data = null;
	private String sCourse = null;
	
	
	
	private JComboBox getcBSelCourse(AdminMainMenu amm)
	{
		std = new StudentClass();
		data = new Vector<RawCourse>();
		
		System.out.println("Test1 Crs Name "+ amm.user.DataForEx.RawCourses.get(0).CourseName);
		System.out.println("Test2 std Name "+ amm.user.GetName());
		
		data= amm.user.DataForEx.RawCourses;
		int i;
		for( i=0;i<amm.user.DataForEx.StudentsWithCourses.size();i++)
		{
			if(amm.user.GetName().endsWith(amm.user.DataForEx.StudentsWithCourses.get(i).Sid))
			{
				std=amm.user.DataForEx.StudentsWithCourses.get(i);
			break;
			}
				
		}
		System.out.println("Test5 std Name "+ std.FirstName +" "+ std.LastName +" "+ std.Sid);
		System.out.println("Test3 Std "+ amm.user.DataForEx.StudentsWithCourses.get(i).ListOfCourses.lOfCourses.size());
		System.out.println("Test4 Std "+ std.ListOfCourses.lOfCourses.size());
		
			
		for( i=0;i<std.ListOfCourses.lOfCourses.size();i++)
		{
			for(int j=0;j<data.size();j++)
			{
				if(data.get(j).Cid.equalsIgnoreCase(std.ListOfCourses.lOfCourses.get(i).getCid()))
					data.remove(j);
				
			}
		}
		System.out.println("All is good yeahhhhhhhhhhh");
		
		if(cBSelCourse==null)
		{
			
			Vector<String> convert = new Vector<String>();
			for(i=0;i<data.size();i++)
			{
				convert.add(data.get(i).CourseName+" "+data.get(i).Cid+" "+data.get(i).ClassID );
			}
			cBSelCourse = new JComboBox(convert);
			cBSelCourse.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					sCourse = cBSelCourse.getSelectedItem().toString();
					System.out.println(sCourse);
					//TODO show all the courses that the student isnt registered to and he graduated from the intro courses needed to study this courses

				}
			});
			cBSelCourse.setBounds(99, 193, 155, 20);
			
		}
		
		return cBSelCourse;
	}
	
	private JButton getbtnRequestAuthorization()
	{
		if(btnRequestAuthorization==null)
		{
			btnRequestAuthorization = new JButton("Request authorization");
			btnRequestAuthorization.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					

					String[] selsectedCourse=sCourse.split(" ");//[1] - cid
					//System.out.println("The selected course is: "+ selsectedCourse[1]);
					String[] selectedStud=amn.user.GetName().toString().split(" ");
					amn.user.DataForEx.RequestCreated=false;
					amn.myLoginFrame.client.handleMessageFromClientUI("CreateExReqAddStudToCourse "+selsectedCourse[1]+" "+selectedStud[selectedStud.length-1]+" "+selsectedCourse[2]);
					while(!amn.user.DataForEx.RequestCreated);
					JOptionPane.showMessageDialog(selCrsForExAdd, "Request sent to Principle");
					

				}
			});
			btnRequestAuthorization.setBounds(342, 237, 139, 23);
			
		}
		
		return btnRequestAuthorization;
	}
	
	private void initialize(AdminMainMenu amm)
	{
		setLayout(null);
		
		setSize(new Dimension(600, 600));
		
		 lblExceptionalRequest = new JLabel("Exceptional request - Add student to course");
		lblExceptionalRequest.setBounds(131, 11, 238, 14);
		
		
		 lblSelectCourse = new JLabel("Select course:");
		lblSelectCourse.setBounds(10, 196, 68, 14);
		
		this.add(lblSelectCourse);
		this.add(lblExceptionalRequest);
		this.add(getcBSelCourse(amm));
		this.add(getbtnRequestAuthorization());
	}
	
	
	
	
	
	
	public StudentAdditionToCourse(AdminMainMenu amm) {
		super();
		initialize(amm);
		
	}
}
